//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmHestonEquityPart.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Methods/Finitedifferences/Utilities/FdmQuantoHelper.h>
#include <gen/QL/Methods/Finitedifferences/Operators/TripleBandLinearOp.h>
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ qTS, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmQuantoHelper^ quantoHelper) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmMesher^ _Cmesher;
    CYieldTermStructure^ _CrTS;
    CYieldTermStructure^ _CqTS;
    CFdmQuantoHelper^ _CquantoHelper;
    try
    {
#ifdef HANDLE
        _phFdmHestonEquityPart = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _CrTS = safe_cast<CYieldTermStructure^> (rTS);
        _CrTS->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _rTS = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_CrTS->GetShared ()); 
        _CqTS = safe_cast<CYieldTermStructure^> (qTS);
        _CqTS->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _qTS = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_CqTS->GetShared ()); 
        _CquantoHelper = safe_cast<CFdmQuantoHelper^> (quantoHelper);
        _CquantoHelper->Lock();
        boost::shared_ptr<QuantLib::FdmQuantoHelper>& _quantoHelper = static_cast<boost::shared_ptr<QuantLib::FdmQuantoHelper>&> (_CquantoHelper->GetShared ()); 
        _ppFdmHestonEquityPart = new boost::shared_ptr<QuantLib::FdmHestonEquityPart> (new QuantLib::FdmHestonEquityPart ( _mesher,  _rTS,  _qTS,  _quantoHelper ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_CrTS != nullptr) _CrTS->Unlock();
        if (_CqTS != nullptr) _CqTS->Unlock();
        if (_CquantoHelper != nullptr) _CquantoHelper->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (boost::shared_ptr<QuantLib::FdmHestonEquityPart>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonEquityPart = NULL;
#endif
	_ppFdmHestonEquityPart = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (QuantLib::FdmHestonEquityPart& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmHestonEquityPart = NULL;
#endif
	_ppFdmHestonEquityPart = new boost::shared_ptr<QuantLib::FdmHestonEquityPart> (&childNative);
    
    _FdmHestonEquityPartOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (CFdmHestonEquityPart^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonEquityPart = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonEquityPart = new boost::shared_ptr<QuantLib::FdmHestonEquityPart> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonEquityPart = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonEquityPart::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (QuantLib::Handle<QuantLib::FdmHestonEquityPart>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonEquityPart = &childNative;
	_ppFdmHestonEquityPart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonEquityPart>>(childNative.currentLink());
    
    _FdmHestonEquityPartOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (QuantLib::Handle<QuantLib::FdmHestonEquityPart> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonEquityPart = &childNative;
	_ppFdmHestonEquityPart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonEquityPart>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::CFdmHestonEquityPart (QuantLib::FdmHestonEquityPart childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonEquityPart = NULL;
#endif
	_ppFdmHestonEquityPart = new boost::shared_ptr<QuantLib::FdmHestonEquityPart> (new QuantLib::FdmHestonEquityPart (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::~CFdmHestonEquityPart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmHestonEquityPart != NULL)
    {
	    delete _ppFdmHestonEquityPart;
        _ppFdmHestonEquityPart = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::!CFdmHestonEquityPart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmHestonEquityPart != NULL)
    {
	    delete _ppFdmHestonEquityPart;
    }
}
QuantLib::FdmHestonEquityPart& Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::GetReference ()
{
    if (_ppFdmHestonEquityPart == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmHestonEquityPart;
}
boost::shared_ptr<QuantLib::FdmHestonEquityPart>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::GetShared ()
{
    if (_ppFdmHestonEquityPart == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmHestonEquityPart;
}
QuantLib::FdmHestonEquityPart* Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::GetPointer ()
{
    if (_ppFdmHestonEquityPart == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmHestonEquityPart;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonEquityPart>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::GetHandle ()
{
	if (_phFdmHestonEquityPart == NULL)
	{
		_phFdmHestonEquityPart = new Handle<QuantLib::FdmHestonEquityPart> (*_ppFdmHestonEquityPart);
	}
	return *_phFdmHestonEquityPart;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::HasNative () 
{
	return (_ppFdmHestonEquityPart != NULL);
}

Cephei::QL::Methods::Finitedifferences::Operators::ITripleBandLinearOp^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::GetMap::get ()
{
    try
    {
    	QuantLib::TripleBandLinearOp& _rv = (QuantLib::TripleBandLinearOp&)(*_ppFdmHestonEquityPart)->getMap ( );   
        Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Operators::CTripleBandLinearOp (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::IFdmHestonEquityPart^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1); //a
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2); //a
    	(*_ppFdmHestonEquityPart)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Operators::IFdmHestonEquityPart^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonEquityPart_Factory::Create (Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Termstructures::IYieldTermStructure^ rTS, Cephei::QL::Termstructures::IYieldTermStructure^ qTS, Cephei::QL::Methods::Finitedifferences::Utilities::IFdmQuantoHelper^ quantoHelper)
{
    return REFNEW CFdmHestonEquityPart ( mesher,  rTS,  qTS,  quantoHelper);
}
